 
package com.zdkj.subway.stm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.common.core.util.R;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.stm.entity.SwStIncomeSettle;
import com.zdkj.subway.stm.mapper.SwStIncomeSettleMapper;
import com.zdkj.subway.stm.service.SwStIncomeSettleService;
import com.zdkj.subway.stm.util.DateUtilStm;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * 收入结算
 *
 * @author Gofe
 * @date 2020-02-12 10:38:29
 */
@Service
public class SwStIncomeSettleServiceImpl extends ServiceImpl<SwStIncomeSettleMapper, SwStIncomeSettle> implements SwStIncomeSettleService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R settle(Date beginTime, Date endTime) {
        Double settleAmount=0.0;
        //todo 后续需要根据原始数据录入模块完成结算金额计算
        /**
         * 根据开始时间和结束时间查询未提交的
         * 1.TVM钱箱清点记录，结算金额(1)=清点金额合计
         * 2.TVM日结账单数据，TVM日结账单金额=(现金支付金额+电子支付金额)
         * 现金支付金额=电子支付金额=(售票总金额+充值总金额+故障退款金额)
         * 结算金额(2)=TVM日结账单金额合计
         * 3.售票员日结算数据，结算金额(3)=(预收款+实收总金额)合计
         * 4.手工清出数据，结算金额(4)=(清出金额硬币+清出金额纸币+非标准币)合计
         * 5.互联网售票机数据，结算金额(6)=小计金额合计
         * 结算金额=结算金额(1)+结算金额(2)+结算金额(3)+结算金额(4)+结算金额(5)
         * */
        SwStIncomeSettle stIncomeSettle = ObjectUtils.getDefaultObj(SwStIncomeSettle.class);
        PigxUser user = SecurityUtils.getUser();
        stIncomeSettle.setSettleStartDate(DateUtilStm.asLocalDateTime(beginTime));
        stIncomeSettle.setSettleEndDate(DateUtilStm.asLocalDateTime(endTime));
        stIncomeSettle.setSettleTime(LocalDateTime.now());
        stIncomeSettle.setSettleAmount(new BigDecimal(settleAmount));
        stIncomeSettle.setIsSubmit(0);
        stIncomeSettle.setSettlePerson(user.getUsername());
        stIncomeSettle.setSettlePersonId(user.getId());
        return R.ok(save(stIncomeSettle));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R settleCommit(Integer id) {
        SwStIncomeSettle stIncomeSettle =getById(id);
        //todo 后续需要根据原始数据录入模块完成此功能
        /**
         * 获取已结算未提交的原始数据
         * 1.TVM钱箱清点记录
         * 2.TVM日结账单数据
         * 3.售票员日结算数据
         * 4.手工清出数据
         * 5.互联网售票机数据
         * 修改状态为已提交
         * */

        stIncomeSettle.setIsSubmit(1);
        return R.ok(updateById(stIncomeSettle));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateOriginalState(Integer state) {
        //todo 后续需要根据原始数据录入模块完成此功能
        /**
         * 获取全部已提交的原始数据
         * 1.TVM钱箱清点记录
         * 2.TVM日结账单数据
         * 3.售票员日结算数据
         * 4.手工清出数据
         * 5.互联网售票机数据
         * 修改状态为已清分
         * */
        return R.ok();
    }
}
